<?php
namespace Home\Controller;
use Common\Controller\HomeBaseController;
/**
 * 后台登陆控制器
 */
class LoginController extends HomeBaseController{

    
	//后台登陆界面
    public function index() {
		//session('member_id',NULL); 
        $member_id=session('member_id');
		//print_r($_COOKIE);exit;
    	if(!empty($member_id)){//已经登录
    		redirect(U("Home/Member/index"));
    		//redirect(U("Home/Member/agreement"));
    	}else{
				//首页轮播图
		$Banner =M('Banner');	
		$banners =$Banner->field('adv_pic')->where('adv_site=1')->select();

	
		$this->assign('banners',$banners);
      	    $this->display();
     	}
    }
    public function agreement(){
		
		$agreement=$_SESSION['agreement'];
		if(empty($agreement)){
    		redirect(U("Home/Member/index"));
    	
    	}else{
			
			if($_POST){
				$data=M('member')->where('member_id='.$agreement)->find();
				// $_SESSION=array(
    //                 'member_id'=>$data['member_id'],
    //                 'member_name'=>$data['member_name']
    //                 );
				$_SESSION['member_id']=$data['member_id'];
				$_SESSION['member_name']=$data['member_name'];
					$updata_data['last_login_ip']=get_client_ip(0,true);
    			$_SESSION['last_login_time']=$data['last_login_time'];
				$v=get_rand_number(1,10,6);
				$v=implode('',$v);
				
					$_SESSION['verinfo']=$v;
				
				
    			$updata_data['last_login_time']=time();
    			$updata_data['verinfo']=$v;
				
				
				$condition['member_id']=$data['member_id'];
				
				if($remember){
				cookie("member_name",$data['member_name'],3600*24*30);
				cookie("member_pwd",I('post.password'),3600*24*30);
				}
				 
			$result=M('member')->where($condition)->save($updata_data);	
			die(json_encode(array('result'=>1,'message'=>'登录成功、前往管理后台')));
			//redirect(U("Home/Member/index"));	
			}
      	  	$this->display('member-agreement');
     	
			
		}
	}
    public function logout(){
    	session('member_id',NULL);
		session('member_name',NULL);
     	redirect('/');
    }
     
	 
	public function dologin(){
		
		// 做一个简单的登录 组合where数组条件 
            $map['member_name']=I('post.name');

            $map['member_password']=md5(I('post.pwd'));
			$pan=I('post.pan');
			//print_r(I('post'));exit;
            $data=M('member')->where($map)->find();
			$code=I('post.code'); 
			 if ($this->check_verify($code)){
				 
			 }else{
				 die(json_encode(array('result'=>0,'message'=>'验证码错误！')));
			 }
			 
			
            if (empty($data)) {
                //$this->error('账号或密码错误');

				die(json_encode(array('result'=>0,'message'=>'账号或密码错误')));
            }else{
            	 if($data['status']==0){
                	die(json_encode(array('result'=>0,'message'=>'您的账号被禁用')));
                }
                $_SESSION['agreement']=$data['member_id'];
				$update_array['pan']=$pan;
				
				M('member')->where('member_id='.$data['member_id'])->save($update_array);

				die(json_encode(array('result'=>1,'message'=>'登录成功、前往管理后台')));
               // $this->success('登录成功、前往管理后台',U('Home/Member/index'));
            }
		} 

		public function agent_dologin(){
		
		// 做一个简单的登录 组合where数组条件 
            $map['member_name']=I('post.name');

            $map['member_password']=md5(I('post.pwd'));
			$map['is_agent']=1;
            $data=M('member')->where($map)->find();
			//print_r($data);exit;
            if (empty($data)) {
                //$this->error('账号或密码错误');

				die(json_encode(array('result'=>0,'message'=>'账号或密码错误')));
            }else{
            	 if($data['status']==0){
                	die(json_encode(array('result'=>0,'message'=>'您的账号被禁用')));
                }
				$update_array['agent_last_login_time']=time();
               /*  $_SESSION=array(
                    'agent_id'=>$data['member_id'],
                    'agent_name'=>$data['member_name'],
                    'last_login_time'=>$data['last_login_time']
                    ); */
					
					$_SESSION['agent_id']=$data['member_id'];
					$_SESSION['agent_name']=$data['member_name'];
					$_SESSION['agent_last_login_time']=$data['agent_last_login_time'];
				$update_array['last_login_ip']=get_client_ip(0,true);
				
				M('member')->where('member_id='.$data['member_id'])->save($update_array);

				die(json_encode(array('result'=>1,'message'=>'登录成功、前往管理后台')));
               // $this->success('登录成功、前往管理后台',U('Home/Member/index'));
            }
		} 

		//代理登录
		public function agent_login(){
			
			$this->display();
		}

	//检测验证码
	function check_verify($code,$id=1) {
	    $Verify =new \Think\Verify();
	    $Verify->reset = false;
		
	    return $Verify->check($code, $id);
	
	}
}
